﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CodeGenLib;
using System.IO;

namespace TestCodeGenLib
{
    class TestGenCodeLogic
    {
        public void Test()
        {
            this.TestWithBaseAndInclude();
            this.TestWithoutBaseAndInclude();
            this.TestCpp();
        }

        private void TestWithBaseAndInclude()
        {
            GenParam param = new GenParam();
            param.AddBases("Base1");
            param.AddBases("Base2");
            param.AddInclude("Base1.h");
            param.AddInclude("Base2.h");
            param.Class = "Test";
            param.HeaderGuard = "Test_With_Base_And_Include_h";
            param.HFilename = "test.h";
            param.Namespace = "test";

            Codes codes = new Codes();

            GenCodeLogic logic = new GenCodeLogic();
            logic.GenHFile(param, codes);

            using (StreamReader sr = File.OpenText("TestWithBaseAndInclude.h"))
            {
                List<String>.Enumerator code = codes.Lines.GetEnumerator();
                while (!sr.EndOfStream)
                {
                    String desire = sr.ReadLine();
                    code.MoveNext();

                    if (desire != code.Current)
                    {
                        String msg = "TestWithBaseAndInclude Failed - ";
                        msg += "\ndesire: " + desire;
                        msg += "\nline: " + code.Current;
                        throw new Exception(msg);
                    }
                }
            }

            Console.WriteLine("TestWithBaseAndInclude: Succeed");
        }

        private void TestWithoutBaseAndInclude()
        {
            GenParam param = new GenParam();
            param.Class = "Test";
            param.HeaderGuard = "Test_Without_Base_And_Include_h";
            param.Namespace = "test";

            Codes codes = new Codes();

            GenCodeLogic logic = new GenCodeLogic();
            logic.GenHFile(param, codes);

            using (StreamReader sr = File.OpenText("TestWithoutBaseAndInclude.h"))
            {
                List<String>.Enumerator code = codes.Lines.GetEnumerator();
                while (!sr.EndOfStream)
                {
                    String desire = sr.ReadLine();
                    code.MoveNext();

                    if (desire != code.Current)
                    {
                        String msg = "TestWithoutBaseAndInclude Failed - ";
                        msg += "desire: " + desire;
                        msg += "line: " + code.Current;
                        throw new Exception(msg);
                    }
                }
            }

            Console.WriteLine("TestWithoutBaseAndInclude: Succeed");
        }

        private void TestCpp()
        {
            GenParam param = new GenParam();
            param.Class = "Test";
            param.HFilename = "Test";
            param.Namespace = "test";

            Codes codes = new Codes();

            GenCodeLogic logic = new GenCodeLogic();
            logic.GenCppFile(param, codes);

            using (StreamReader sr = File.OpenText("Test.cpp"))
            {
                List<String>.Enumerator code = codes.Lines.GetEnumerator();
                while (!sr.EndOfStream)
                {
                    String desire = sr.ReadLine();
                    code.MoveNext();

                    if (desire != code.Current)
                    {
                        String msg = "TestCpp Failed - ";
                        msg += "desire: " + desire;
                        msg += "line: " + code.Current;
                        throw new Exception(msg);
                    }
                }
            }

            Console.WriteLine("TestCpp: Succeed");
        }
    }
}
